package com.loong.common.alipay.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayResponse;
import com.alipay.api.request.*;
import com.loong.common.alipay.param.*;
import com.loong.common.alipay.result.*;
import com.loong.common.validator.group.CommonValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/11/19 10:59
 */
@Slf4j
public class AlipayUtil {

    /**
     * App支付接口：外部商户APP唤起快捷SDK创建订单并支付
     * @param param app支付参数
     * @return app支付的结果
     * @throws AlipayApiException
     */
    public static AppPayResult tradeAppPayService(@Validated(value = CommonValidate.class)AppPayParam param) throws AlipayApiException {
        AlipayTradeAppPayRequest appPayRequest = new AlipayTradeAppPayRequest();
        //设置业务参数，bizContent为发送的请求信息
        appPayRequest.setBizContent(JSON.toJSONString(param));

        //处理请求
        AlipayClient alipayClient = AlipayClientUtil.getAlipayClient();
        AlipayResponse alipayResponse = alipayClient.sdkExecute(appPayRequest);
        String body = alipayResponse.getBody();
        return JSON.parseObject(body, AppPayResult.class);
    }

    /**
     * 统一收单交易关闭接口：
     *    用于交易创建后，用户在一定时间内未进行支付，可调用该接口直接将未付款的交易进行关闭
     * @param param 交易关闭参数
     * @return 支付关闭的结果
     * @throws AlipayApiException
     */
    public static PayCloseResult tradeCloseService(PayCloseParam param) throws AlipayApiException {
        AlipayTradeCloseRequest closeRequest = new AlipayTradeCloseRequest();
        //设置业务参数，bizContent为发送的请求信息
        closeRequest.setBizContent(JSON.toJSONString(param));

        //处理请求
        AlipayClient alipayClient = AlipayClientUtil.getAlipayClient();
        AlipayResponse alipayResponse = alipayClient.execute(closeRequest);
        String body = alipayResponse.getBody();
        return JSON.parseObject(body, PayCloseResult.class);
    }

    /**
     * 统一收单线下交易查询：
     *     提供所有支付宝支付订单的查询，商户可以通过该接口主动查询订单状态
     * @param param 交易查询参数
     * @return 交易查询结果
     * @throws AlipayApiException
     */
    public static TradeQueryResult tradeQueryService(TradeQueryParam param) throws AlipayApiException {
        AlipayTradeQueryRequest queryRequest = new AlipayTradeQueryRequest();
        //设置业务参数，bizContent为发送的请求信息
        queryRequest.setBizContent(JSON.toJSONString(param));

        //处理请求
        AlipayClient alipayClient = AlipayClientUtil.getAlipayClient();
        AlipayResponse alipayResponse = alipayClient.execute(queryRequest);
        String body = alipayResponse.getBody();
        return JSON.parseObject(body, TradeQueryResult.class);
    }

    /**
     * 统一收单交易退款接口：
     *     当交易发生之后，由于买家或者卖家的原因需要退款时，卖家可以通过退款接口将支付款退还给买家
     * @param param 退款参数
     * @return 退款结果
     */
    public static RefundResult tradeRefundService(@Validated(value = CommonValidate.class)RefundParam param) throws AlipayApiException {
        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        //设置业务参数，bizContent为发送的请求信息
        refundRequest.setBizContent(JSON.toJSONString(param));

        //处理请求
        AlipayClient alipayClient = AlipayClientUtil.getAlipayClient();
        AlipayResponse alipayResponse = alipayClient.execute(refundRequest);
        String body = alipayResponse.getBody();
        return JSON.parseObject(body, RefundResult.class);
    }

    /**
     * 统一收单交易退款查询：
     *     商户可使用该接口查询自已提交的退款请求是否执行成功
     * @param param 退款查询参数
     * @return
     */
    public static RefundQueryResult tradeFastPayRefundQueryService(@Validated(value = CommonValidate.class)RefundQueryParam param) throws AlipayApiException {
        AlipayTradeFastpayRefundQueryRequest queryRequest = new AlipayTradeFastpayRefundQueryRequest();
        //设置业务参数，bizContent为发送的请求信息，开发者需要根据实际情况填充此类
        queryRequest.setBizContent(JSON.toJSONString(param));

        //处理请求
        AlipayClient alipayClient = AlipayClientUtil.getAlipayClient();
        AlipayResponse alipayResponse = alipayClient.execute(queryRequest);
        String body = alipayResponse.getBody();
        return JSON.parseObject(body, RefundQueryResult.class);
    }

}
